package com.mall.gateway.config;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.UUID;

/**
 * Feign安全配置类
 * 用于为Feign客户端添加安全认证头，解决服务间调用401问题
 */
@Slf4j
@Configuration
public class FeignSecurityConfig {

    /**
     * Feign请求拦截器
     * 为内部服务调用添加认证头
     *
     * @return RequestInterceptor
     */
    @Bean
    public RequestInterceptor feignRequestInterceptor() {
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate template) {
                // 添加内部服务调用标识
                template.header("X-Internal-Service", "true");
                template.header("X-Service-Name", "mall-gateway");
                
                // 添加追踪ID
                template.header("X-Trace-Id", UUID.randomUUID().toString());
                
                // 添加内部认证Token
                template.header("Authorization", "Bearer internal-service-token");
            }
        };
    }
}